function categoryList(){
    $('#main').html(getCategoryListDemo());
    dotlist = $('#dotlist').html();
    var offset = 0;
    data = {
        'offset': offset,
        'limit': 0,
        'getall': 1,
    }
    var api = '/backstage/sutra/category/list';
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: 'GET',
        data: data,
        async: false,
        dataType: 'json',
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status==200){
                var doTtmpl = doT.template(dotlist);
                $('#categorylist').html(doTtmpl(data));
            }
            else{
                alert('请求异常, 请刷新')
            }
        },
    });
}


function categoryCreate(){
    $('#main').html(getCategoryCreateDemo());
    $('#create_category').on('click', function(){
        category_name = $('#category_name').val();
        category_desc = $('#category_desc').val();
        category_img = $('#category_img').val();
        if (!category_name || !category_desc || !category_img) {
            return
        };
        var csrftoken = getCookie('csrftoken');
        $.ajax({
            url: '/backstage/sutra/category',
            type: 'POST',
            data: new FormData($('#create_category_form')[0]),
            async: false,
            processData: false,
            contentType: false,
            cache: false,
            beforeSend: function(xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            },
            success: function(data, status){
                if(data.status==200){
                    alert('创建成功');
                    history.back();
                }
                else{
                    alert('请求异常, 请刷新')
                }
            },
        });
    });
}


function categoryGet (Id) {
    var category = false;
    var api = '/backstage/sutra/category/' + Id;   
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: 'GET',
        async: false,
        dataType: 'json',
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status==200){
                category = data.data
            }
            else{
                alert('请求异常, 请重试')
            }
        },
    });
    return category
}


function categoryUpdate(Id){
    $('#main').html(getCategoryUpdateDemo());
    category = categoryGet(Id);
    $('#category_name').val(category.name);
    $('#category_desc').val(category.desc);
    $('#category_img').attr('src', category.cover_img_url);

    $('#create_category').on('click', function(){
        var csrftoken = getCookie('csrftoken');
        $.ajax({
            url: '/backstage/sutra/category/' + Id,
            type: 'PUT',
            data: new FormData($('#create_category_form')[0]),
            async: false,
            processData: false,
            contentType: false,
            cache: false,
            beforeSend: function(xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            },
            success: function(data, status){
                if(data.status==200){
                    alert('修改成功');
                    location.href = 'category.html?operation=list';
                }
                else{
                    alert('请求异常, 请重试')
                }
            },
        });
    });
}


function categoryDelete(Id){
    var api = '/backstage/sutra/category/' + Id;
    msg = '您确定要删除该经书类别吗?'
    if(confirm(msg)!=true){
        return
    }
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: 'DELETE',
        async: true,
        dataType: 'json',
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status==200){
                alert('删除成功');
                location.href = 'category.html?operation=list';
            }
            else{
                alert('请求异常, 请重试')
            }
        },
    });
}



$(document).ready(function(){
    operation = getQueryString('operation');
    switch(operation)
    {
        case 'list':
          categoryList();
            break;
        case 'create':
          categoryCreate();
            break;
        default:
           categoryList();
           break;
    }
})